package com.zjinyin.forum.service.impl;

import com.zjinyin.forum.common.AppResult;
import com.zjinyin.forum.common.ResultCode;
import com.zjinyin.forum.dao.BoardMapper;
import com.zjinyin.forum.exception.ApplicationException;
import com.zjinyin.forum.model.Board;
import com.zjinyin.forum.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-08-10
 * Time: 11:41
 */

/**
 * 对版块功能接口中的具体功能实现
 */

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    /**
     * 查询数据库中板块表里的按照升序排列的前 num 个版块
     *
     * @param num 指定数目
     * @return 返回版块集合
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        // 1. 校验参数
        if (num == null || num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 2. 根据 num 查询数据库版块数
        return boardMapper.selectByNum(num);
    }

    /**
     * 给定 id 编号查询指定版块
     *
     * @param id 指定 id 版块编号
     * @return 返回指定版块信息
     */
    @Override
    public Board selectById(Long id) {
        // 1. 参数校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_USER_COUNT_ARTICLE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_COUNT_ARTICLE));
        }

        // 2. 查询数据库
        Board board = boardMapper.selectByPrimaryKey(id);

        // 3. 校验 board
        if (board == null) {
            log.warn("board id : " + id + " " + ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        log.info("查询到 board id=" + id + " 的版块");

        return board;
    }

    /**
     * 给指定 id 版块下的文章总数 + 1 ( 更新 )
     *
     * @param id 版块 id
     */
    @Override
    public void increaseOneArticleCountById(Long id) {
        // 1. 参数校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_USER_COUNT_ARTICLE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_COUNT_ARTICLE));
        }

        // 2. 根据 id 查询是否存在指定的版块
        Board board = boardMapper.selectByPrimaryKey(id);

        // 3. 判断版块
        if (board == null) {
            log.warn("board id : " + id + " " + ResultCode.FAILED_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }

        // 4. 更新这个指定 id 版块下的文章数
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        if (row != 1) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString() + " , 受影响行数 > 1 错误");
        }
    }


    /**
     * 删除指定板块下的文章, 指定版块文章数 -1
     * @param id 版块 id
     */
    @Override
    public void deleteOneArticleCountById(Long id) {
        // 1. 参数校验
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_USER_COUNT_ARTICLE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_COUNT_ARTICLE));
        }

        // 2. 根据 id 查询指定版块
        Board board = boardMapper.selectByPrimaryKey(id);

        // 3. 检验版块
        if(board == null) {
            log.warn("boardId : " + id + " " + ResultCode.FAILED_BOARD_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        // 4. 创建版块新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        updateBoard.setUpdateTime(new Date());

        if(updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }

        // 5. 写回数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);

        // 6. 检验结果
        if(row != 1) {
            log.warn(ResultCode.FAILED + " , 受影响行数不为 1 错误");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED) + " , 受影响行数不为 1 错误");
        }

        log.info("版块数目减 1 成功");

    }
}
